package model;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import database.SingleEntityManagerFactory;
import database.SingleEntityManager;

public class Model {
	
	int id;
		
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public void add()
	{
		SingleEntityManager.getInstance().persist(this);
	}
	
	public void remove()
	{
		SingleEntityManager.getInstance().remove(this);
	}

	public void update(Model m)
	{
		SingleEntityManager.getInstance().merge(m);
	}
	
	public void commit()
	{
		SingleEntityManager.getInstance().getTransaction().begin();
		SingleEntityManager.getInstance().getTransaction().commit();
	}
	
	public void closeTransaction()
	{
		SingleEntityManager.getInstance().close();
	}
	
	
	public List<?> getList()
	{
		List<Model> list2;
		Query query = SingleEntityManager.getInstance().createQuery("FROM "+this.getClass().getCanonicalName());
		list2 = query.getResultList();
		return list2;	 
	}
	
	
	

}
